package com.hualan.web;

import com.hualan.service.service.service.ShaixuanService;
import com.hualan.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@RequestMapping("/shaixuan")
public class ShaixuanController {
    @Autowired
    private ShaixuanService shaixuanService;

    @RequestMapping("/listAll")
    public ResultEntity listAll(){
        return ResultEntity.ok(shaixuanService.listAll());
    }


    @RequestMapping("/shaiXuan")
    public ResultEntity shaiXuan(String diyu, Integer zixun, String level, String  result){
        if (result!=null && result.length()>2){
            result = result.substring(1,result.length()-1);
            String[] split = result.split(",");
            Integer[] array = new Integer[split.length];
            for (int i = 0; i < split.length; i++) {
                array[i] = Integer.parseInt(split[i]);
            }
            if (diyu.length()>0){
                return ResultEntity.ok(shaixuanService.shaiXuan(diyu,zixun,level, Arrays.asList(array)));
            }else {
                return ResultEntity.ok(shaixuanService.shaiXuan(null,zixun,level, Arrays.asList(array)));
            }

        }else{
            if (diyu.length()>0){
                return ResultEntity.ok(shaixuanService.shaiXuan(diyu,zixun,level, null));
            }else {
                return ResultEntity.ok(shaixuanService.shaiXuan(null,zixun,level, null));
            }
        }

    }
}
